package com.wzdigit.wms.raw.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.raw.constant.SyncTypeConstant;
import com.wzdigit.wms.raw.domain.dto.IfAsnHeaderDto;
import com.wzdigit.wms.raw.domain.mid.IfReviewresult;
import com.wzdigit.wms.raw.exception.SyncException;
import com.wzdigit.wms.raw.mapper.mid.IfReviewresultMapper;
import com.wzdigit.wms.raw.service.mid.IfAsnHeaderService;
import com.wzdigit.wms.raw.service.mid.IfReviewResultService;
import com.wzdigit.wms.raw.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**OA评审结果回写接口
    * @author shuaic
    * @date 2021/8/10 20:52
*/
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfReviewResultTask {


    @Autowired
    IfReviewResultService ifReviewResultService ;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    final String syncType = SyncTypeConstant.IF_ASN_TO_ASN;

    @Scheduled(cron = "0 0/5 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("IfReviewResultTask start...");
            long start = System.currentTimeMillis();
            List<IfReviewresult> datas = ifReviewResultService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas);
            }
            log.info("------IfReviewResultTask end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }

    public void syncData(List<IfReviewresult> datas) {
        for (IfReviewresult data : datas) {
            try {
                switch (data.getVcode()){
                    case "A":
                        ifReviewResultService.changeGoodsQaStatus(data);
                        break;
                    case "B":
                        ifReviewResultService.syncData(data);
                        break;
                    case "C":
                        ifReviewResultService.changeGoodsQaStatus(data);
                        break;
                    case "D":
                        ifReviewResultService.changeGoodsQaStatus(data);
                        break;
                }
            } catch (Exception e) {
                log.warn("OA凭证同步回写失败 批次号 ",data.getPrueflos());
                log.warn("OA凭证同步回写失败 批次号" + e.getMessage() == null ? e.toString() : e.getMessage());
                String message = "";
                if (SyncException.class.isInstance(e)) {
                    SyncException ex = (SyncException) e;
                    message = ex.getFailedReason();
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    log.info("【OA凭证同步回写失败】" + ex.getFailedReason());
                    syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(),message);
                } else {
                    e.printStackTrace();
                    if (e.getMessage() == null) {
                        message = e.getClass().getName();
                    } else {
                        message = e.getMessage();
                    }
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    syncErrorDataService.saveSyncErrorData(syncType, data.getPrueflos(), message);
                }
                e.printStackTrace();
            }
        }
    }
}
